package com.starlight.easybuyprictice.controller;

import com.starlight.easybuyprictice.interceptor.FreeTokenAuthentication;
import com.starlight.easybuyprictice.interceptor.RoleAuthentication;
import com.starlight.easybuyprictice.service.GoodsManageService;
import com.starlight.easybuyprictice.service.GoodsService;
import com.starlight.easybuyprictice.service.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

import static com.starlight.easybuyprictice.entity.Token.Type.TT_ADMIN;

/**
 * @Author yfy2001
 * @date 2022/11/29 11:58
 */
@Controller
@RequestMapping("goods")
public class GoodsController {

    private GoodsManageService goodsManageService;
    private GoodsService goodsService;
    @Autowired
    public GoodsController setGoodsManageService(GoodsManageService goodsManageService,GoodsService goodsService){
        this.goodsManageService = goodsManageService;
        this.goodsService=goodsService;
        return this;
    }
    @RequestMapping("category/list/parents")
    @ResponseBody
    @FreeTokenAuthentication
    public Result listCategories() {
 //System.out.println("list categories" );
        return goodsManageService.listParentCategories();
    }

    @RequestMapping("category/update")
    @ResponseBody
    @FreeTokenAuthentication
    public Result updateCategory(int parentId,String oldName,String newName){
        return goodsManageService.updateGoodsCategories(parentId,oldName,newName);
    }

    @RequestMapping("category/list/nested")
    @ResponseBody
    @FreeTokenAuthentication
    public Result listNestedCategories() {
        return goodsManageService.listNestedGoodCategories();
    }

    @RequestMapping(value="category/delete/{catId}",method= RequestMethod.DELETE)
    @ResponseBody
//    @RoleAuthentication(roles = TT_ADMIN)
    public Result deleteCategory(@PathVariable("catId")int catId) {
        return goodsManageService.deleteCategory(catId);
    }

    @PostMapping("category/addCategory")
    @ResponseBody
    @FreeTokenAuthentication
    public Result addCategory(String name,Integer parentId){
        System.out.println(parentId);
        return goodsManageService.addCategory(name,parentId);
    }

    /**
     * 查询单个商品详细信息
     * @param goodsId
     * @return
     */
    @RequestMapping("goodView/{goodsId}")
    @ResponseBody
    @FreeTokenAuthentication
    public Result findGoodById(@PathVariable("goodsId") int goodsId){
        return  goodsService.findGoodById(goodsId);
    }

    /**
     * 后台获取商品列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    @FreeTokenAuthentication
    public Result listGoods(){
        return  goodsService.listGoods();
    }

    /**
     * 后台获取所有子分类
     * @return
     */
    @RequestMapping("/childrenCat")
    @ResponseBody
    @FreeTokenAuthentication
    public Result childrenCats(){
        return goodsManageService.listChildrenGoodCategories();
    }

    /**
     * 随机查询部分商品信息
     * 首页展示热卖
     * @param count
     * @return
     */
    @RequestMapping("/randomList/{count}")
    @ResponseBody
    @FreeTokenAuthentication
    public Result listGoodsRandom(@PathVariable("count") int count){
        return  goodsService.listGoodsRandom(count);
    }

    /**
     * 列出分类下所有商品
     * @param catId
     * @return
     */
    @RequestMapping("/catList/{catId}")
    @ResponseBody
    @FreeTokenAuthentication
    public Result listSameCatGoods(@PathVariable("catId") Integer catId){
        return  goodsService.listSameCatGoods(catId);
    }

    //添加商品
    @PostMapping("/addGood")
    @ResponseBody
    @FreeTokenAuthentication
    public Result addGood(String name,int cat_id,BigDecimal price,int count,String description){
        return goodsService.addGood(name,cat_id,price,count,description);
    }

    /**
     * 修改商品
     * @param name
     * @param catId
     * @param price
     * @param count
     * @param description
     * @return
     */
    @PostMapping("/updateGood")
    @ResponseBody
    @FreeTokenAuthentication
    public Result updateGood(Integer id, String name, int catId, BigDecimal price, int count, String description){
        return goodsService.updateGood(id,name,catId,price,count,description);
    }

    /**
     * 删除商品
     * @param goodId
     * @return
     */
    @RequestMapping("/deleteGood/{goodId}")
    @ResponseBody
    @FreeTokenAuthentication
    public Result deleteGood(@PathVariable("goodId") Integer goodId){
        return goodsService.deleteGoodById(goodId);
    }
}

